<?php

/*
 * @author Ebrain Cedeno <ebraincedeno@gmail.com>
 */
class ClientController extends Controller{
    
    public function actionGrid(){
        
        
        VeCms::AccessValidate(array('admin'=>array(0,1)));
        
        if (isset($_GET['del'])){
            $this->validate = new TForm();
            TForm::addMessage("eliminado", "Se ha eliminado el cliente indicado correctamente");
            $this->acction_del = true;
        }
        else{
            $this->acction_del = false;
        }
        
        $client = new client();
        if (isset($_POST["search"])){
            $client->setAttributes($_POST);
            $client->search();
        }
        
        
        $this->form = new TForm();
        $this->form->Method = 'POST';
        $this->form->Action =  VeCms::createUrl('./grid');
        
        $this->button_new_client = new TButton("Nuevo Cliente");
        $this->button_new_client->Width = 125;
        
        $js_rol = '$(location).attr("href","../client/new");';
        
        $this->button_new_client->onClick = $js_rol;
        $this->button_new_client->OptionsAditional = array('style'=>array('font-weight:bold',
                                                                        'cursor:pointer',
                                                                        'border:1px solid gray',
                                                                        'background-image:url('.VeCms::getPathImage().VeCmsEnum::ICON_ADD.')',
                                                                        'background-position:left',
                                                                        'background-repeat: no-repeat',
                                                                        'background-position-x:5px',
                                                                        'text-align: right'));
        
        
        
        
        $offset = (isset($_GET['offset']))?$_GET['offset']:0;
        
        $client->Limit = 10;
        $client->Offset = $offset;
        
        
        $this->client_grid = new TTable();
        $this->client_grid->Id = 'grid-user';
        $this->client_grid->Columns = 9;
        $this->client_grid->Rows = $client->Limit;
        $this->client_grid->Width = "100%";
        $this->client_grid->Headers=array('Nro','Nombre','Rif','Telefono','Celular','Correo','','','');
        $this->client_grid->WidthCell[0] = 25;
        $this->client_grid->WidthCell[1] = 180;
        $this->client_grid->WidthCell[2] = 180;
        $this->client_grid->WidthCell[3] = 180;
        $this->client_grid->WidthCell[4] = 180;
        $this->client_grid->WidthCell[5] = 180;
        $this->client_grid->WidthCell[6] = 25;
        $this->client_grid->WidthCell[7] = 25;
        $this->client_grid->WidthCell[8] = 25;
        $this->client_grid->RowsAlign[0] = 'center'; 
        
        $this->client_grid->Pagination = new TPagination(array("offset"=>$offset,"limit"=>$client->Limit,"total"=>$client->Count("active=:active",array(":active"=>1))));
        
        // generando un filtro para la app
        
        $tedit_business_name = new TEdit();
        $tedit_business_name->Name = "business_name";
        $tedit_business_name->Id = $tedit_business_name->Id;
        $tedit_business_name->Width = 180;
        $tedit_business_name->Height = 25;
        $tedit_business_name->Text = $client->business_name;
        $tedit_business_name->OptionsAditional['style'][]='border:1px solid gray';
        
        $tedit_rif = new TEdit();
        $tedit_rif->Name = "rif";
        $tedit_rif->Id = $tedit_rif->Id;
        $tedit_rif->Width = 180;
        $tedit_rif->Height = 25;
        $tedit_rif->Text = $client->rif;
        $tedit_rif->OptionsAditional['style'][]='border:1px solid gray';
        
        $tedit_phone = new TEdit();
        $tedit_phone->Name = "phone";
        $tedit_phone->Id = $tedit_phone->Id;
        $tedit_phone->Width = 180;
        $tedit_phone->Height = 25;
        $tedit_phone->Text =$client->phone;
        $tedit_phone->OptionsAditional['style'][]='border:1px solid gray';
        
        $tedit_phone_mobile = new TEdit();
        $tedit_phone_mobile->Name = "phone_mobile";
        $tedit_phone_mobile->Id = $tedit_phone->Id;
        $tedit_phone_mobile->Width = 180;
        $tedit_phone_mobile->Height = 25;
        $tedit_phone_mobile->Text = $client->phone_mobile;
        $tedit_phone_mobile->OptionsAditional['style'][]='border:1px solid gray';
        
        $tedit_e_mail = new TEdit();
        $tedit_e_mail->Name = "e_mail";
        $tedit_e_mail->Id = $tedit_phone->Id;
        $tedit_e_mail->Width = 180;
        $tedit_e_mail->Height = 25;
        $tedit_e_mail->Text = $client->e_mail;
        $tedit_e_mail->OptionsAditional['style'][]='border:1px solid gray';
        
        $search = new TButton("");
        $search->Name = "search";
        $search->Id = $search->Name;
        $search->Type = 'submit';
        $search->Width = 28;
        $search->Height =25;
        $search->OptionsAditional['style'][]='background-image:url('.VeCms::getPathImage().VeCmsEnum::ICON_SEARCH.')';
        $search->OptionsAditional['style'][]='background-position:center';
        $search->OptionsAditional['style'][]='background-repeat: no-repeat';
        $search->OptionsAditional['style'][]='border:1px solid gray';
        $search->OptionsAditional['style'][]='cursor:pointer';
        
        
        $this->client_grid->Items[] = array("",$tedit_business_name->show(),
                                                $tedit_rif->show(),
                                                $tedit_phone->show(),
                                                $tedit_phone_mobile->show(),
                                                $tedit_e_mail->show(),
                                                $search->show(),"","");
        
        
        $client_list = $client->findAll("active=:active",array(':active'=>1));
        $count =1;
        foreach ($client_list as $key=>$value){
            
            $view = new TImage();
            $view->Src = VeCms::getPathImage().VeCmsEnum::ICON_VIEW;
            $js_view = '$(location).attr("href","../client/view?id_client='.$value->id_client.'")';
            $view->onClick = $js_view;
            $view->OptionsAditional['style'][] = 'cursor:pointer';
            
            $edit = new TImage();
            $js_edit = '$(location).attr("href","../client/update?id_client='.$value->id_client.'")';
            $edit->onClick = $js_edit;
            $edit->Src = VeCms::getPathImage().VeCmsEnum::ICON_EDIT;
            
            $edit->OptionsAditional['style'][] = 'cursor:pointer';
            
            $delete = new TImage();
            $js_delete = 'if(confirm(\'Esta seguro que desea eliminar este cliente?\')){
                            $(location).attr("href","../client/delete?id_client='.$value->id_client.'");
                          }';
            $delete->onClick = $js_delete;
            $delete->Src = VeCms::getPathImage().VeCmsEnum::ICON_DELETE;
            $delete->OptionsAditional['style'][]='cursor:pointer';
            
            $this->client_grid->Items[]=array($count,
                                                $value->business_name,
                                                $value->rif,
                                                $value->phone,
                                                $value->phone_mobile,
                                                $value->e_mail,
                                                $view->show(),
                                                $edit->show(),
                                                $delete->show());
            $count++;
        }
        
       
        
    }
    
    public function actionNew(){
        
        $client = new client();
        
        if (isset($_POST['guardar'])){
            $client->setAttributes($_POST);
            if ($client->validate()){
                if ($client->save()){
                    TForm::addMessage("registrado", "El cliente fue registrado correctamente");
                    $this->redirec(VeCms::createUrl("./grid"),5);
                }
            }
        }
        
        
        $this->form = new TForm();
        $this->form->Method = 'POST';
        $this->form->Action = VeCms::createUrl("./new");
        
        $this->link_back = new TLink("Regresar");
            $js_link = "parent.history.back(); 
                        return false; 
                        ";
        $this->link_back->onClick = $js_link;
        $this->link_back->OptionsAditional['style'][] = "color:#A9BCF5";
        $this->link_back->OptionsAditional['style'][] = "font-weight:bold";
        
        //generando el formulario de clientes
        
        $this->business_name_label = new TLabel("Nombre:");
        $this->business_name_label->OptionsAditional = array('style'=>array('font-weight:bold'));
        $this->business_name_entry = new TEdit();
        $this->business_name_entry->Name = "business_name";
        $this->business_name_entry->Id = $this->business_name_entry->Name;
        $this->business_name_entry->Width = 250;
        $this->business_name_entry->Height = 25;
        $this->business_name_entry->Type = "text";
        $this->business_name_entry->Text = $client->business_name;
        $this->business_name_entry->OptionsAditional['style'] = array('border:1px solid black');
        
        $this->rif_label = new TLabel("Rif:");
        $this->rif_label->OptionsAditional = array('style'=>array('font-weight:bold'));
        $this->rif_entry = new TEdit();
        $this->rif_entry->Name = "rif";
        $this->rif_entry->Id = $this->rif_entry->Name;
        $this->rif_entry->Width = 250;
        $this->rif_entry->Height = 25;
        $this->rif_entry->Type = "text";
        $this->rif_entry->Text = $client->rif;
        $this->rif_entry->OptionsAditional['style'] = array('border:1px solid black');
        
        $this->address_label = new TLabel("Direccion:");
        $this->address_label->OptionsAditional = array('style'=>array('font-weight:bold'));
        $this->address_textview = new TTextView();
        $this->address_textview->Name = "address";
        $this->address_textview->Id = $this->address_textview->Name;
        $this->address_textview->Cols = "10";
        $this->address_textview->Rows = "10";
        $this->address_textview->Width= 250;
        $this->address_textview->Height=50;
        $this->address_textview->Text = $client->address;
        $this->address_textview->OptionsAditional['style'] = array('border:1px solid black');
        
        $this->phone_label = new TLabel("Telefono:");
        $this->phone_label->OptionsAditional = array('style'=>array('font-weight:bold'));
        $this->phone_entry = new TEdit();
        $this->phone_entry->Name = "phone";
        $this->phone_entry->Id = $this->phone_entry->Name;
        $this->phone_entry->Width = 250;
        $this->phone_entry->Height = 25;
        $this->phone_entry->Type="text";
        $this->phone_entry->Text=$client->phone;
        $this->phone_entry->OptionsAditional['style'] = array('border:1px solid black');
        
        $this->phone_mobile_label = new TLabel("Celular:");
        $this->phone_mobile_label->OptionsAditional = array('style'=>array('font-weight:bold'));
        $this->phone_mobile_entry = new TEdit();
        $this->phone_mobile_entry->Name = "phone_mobile";
        $this->phone_mobile_entry->Id = $this->phone_mobile_entry->Name;
        $this->phone_mobile_entry->Width = 250;
        $this->phone_mobile_entry->Height = 25;
        $this->phone_mobile_entry->Type="text";
        $this->phone_mobile_entry->Text = $client->phone_mobile;
        $this->phone_mobile_entry->OptionsAditional['style'] = array('border:1px solid black');
        
        $this->e_mail_label = new TLabel("Correo:");
        $this->e_mail_label->OptionsAditional = array('style'=>array('font-weight:bold'));
        $this->e_mail_entry = new TEdit();
        $this->e_mail_entry->Name = "e_mail";
        $this->e_mail_entry->Id = $this->e_mail_entry->Name;
        $this->e_mail_entry->Width = 250;
        $this->e_mail_entry->Height = 25;
        $this->e_mail_entry->Type="text";
        $this->e_mail_entry->Text = $client->e_mail;
        $this->e_mail_entry->OptionsAditional['style'] = array('border:1px solid black');
        
        $this->button_cancel = new TButton("Cancelar");
        $this->button_cancel->Type = "reset";
        $this->button_cancel->Id = "cancelar";
        $this->button_cancel->Name = $this->button_cancel->Name;
        $this->button_cancel->OptionsAditional = array('style'=>array('font-weight:bold','cursor:pointer'));
        
        $this->button_guardar = new TButton("Guardar");
        $this->button_guardar->Type = 'submit';
        $this->button_guardar->Name = "guardar";
        $this->button_guardar->Id = $this->button_guardar->Name;
        $this->button_guardar->OptionsAditional = array('style'=>array('font-weight:bold','cursor:pointer'));
    }
    
    public function actionUpdate(){
        
        $clientClass = new client();
        
        if (isset($_POST['actualizar'])){
            $clientClass->setAttributes($_POST);
            if ($clientClass->validate()){
                if ($clientClass->update()){
                    TForm::addMessage("registrado", "El cliente fue actualizado correctamente");
                    $this->redirec(VeCms::createUrl("./grid"),5);
                }
            }
        }
        
        $client = $clientClass->find("id_client=:id_client",array(":id_client"=>$_GET['id_client']));
        
        $this->form = new TForm();
        $this->form->Method = 'POST';
        $this->form->Action = VeCms::createUrl("./update?id_client=".$_GET['id_client']);
        
        $this->link_back = new TLink("Regresar");
            $js_link = "parent.history.back(); 
                        return false; 
                        ";
        $this->link_back->onClick = $js_link;
        $this->link_back->OptionsAditional['style'][] = "color:#A9BCF5";
        $this->link_back->OptionsAditional['style'][] = "font-weight:bold";
        
        //generando el formulario de clientes
        
        $this->business_name_label = new TLabel("Nombre:");
        $this->business_name_label->OptionsAditional = array('style'=>array('font-weight:bold'));
        $this->business_name_entry = new TEdit();
        $this->business_name_entry->Name = "business_name";
        $this->business_name_entry->Id = $this->business_name_entry->Name;
        $this->business_name_entry->Width = 250;
        $this->business_name_entry->Height = 25;
        $this->business_name_entry->Type = "text";
        $this->business_name_entry->Text = $client->business_name;
        $this->business_name_entry->OptionsAditional['style'] = array('border:1px solid black');
        
        $this->rif_label = new TLabel("Rif:");
        $this->rif_label->OptionsAditional = array('style'=>array('font-weight:bold'));
        $this->rif_entry = new TEdit();
        $this->rif_entry->Name = "rif";
        $this->rif_entry->Id = $this->rif_entry->Name;
        $this->rif_entry->Width = 250;
        $this->rif_entry->Height = 25;
        $this->rif_entry->Type = "text";
        $this->rif_entry->Text = $client->rif;
        $this->rif_entry->OptionsAditional['style'] = array('border:1px solid black');
        
        $this->address_label = new TLabel("Direccion:");
        $this->address_label->OptionsAditional = array('style'=>array('font-weight:bold'));
        $this->address_textview = new TTextView();
        $this->address_textview->Name = "address";
        $this->address_textview->Id = $this->address_textview->Name;
        $this->address_textview->Cols = "10";
        $this->address_textview->Rows = "10";
        $this->address_textview->Width= 250;
        $this->address_textview->Height=50;
        $this->address_textview->Text = $client->address;
        $this->address_textview->OptionsAditional['style'] = array('border:1px solid black');
        
        $this->phone_label = new TLabel("Telefono:");
        $this->phone_label->OptionsAditional = array('style'=>array('font-weight:bold'));
        $this->phone_entry = new TEdit();
        $this->phone_entry->Name = "phone";
        $this->phone_entry->Id = $this->phone_entry->Name;
        $this->phone_entry->Width = 250;
        $this->phone_entry->Height = 25;
        $this->phone_entry->Type="text";
        $this->phone_entry->Text=$client->phone;
        $this->phone_entry->OptionsAditional['style'] = array('border:1px solid black');
        
        $this->phone_mobile_label = new TLabel("Celular:");
        $this->phone_mobile_label->OptionsAditional = array('style'=>array('font-weight:bold'));
        $this->phone_mobile_entry = new TEdit();
        $this->phone_mobile_entry->Name = "phone_mobile";
        $this->phone_mobile_entry->Id = $this->phone_mobile_entry->Name;
        $this->phone_mobile_entry->Width = 250;
        $this->phone_mobile_entry->Height = 25;
        $this->phone_mobile_entry->Type="text";
        $this->phone_mobile_entry->Text = $client->phone_mobile;
        $this->phone_mobile_entry->OptionsAditional['style'] = array('border:1px solid black');
        
        $this->e_mail_label = new TLabel("Correo:");
        $this->e_mail_label->OptionsAditional = array('style'=>array('font-weight:bold'));
        $this->e_mail_entry = new TEdit();
        $this->e_mail_entry->Name = "e_mail";
        $this->e_mail_entry->Id = $this->e_mail_entry->Name;
        $this->e_mail_entry->Width = 250;
        $this->e_mail_entry->Height = 25;
        $this->e_mail_entry->Type="text";
        $this->e_mail_entry->Text = $client->e_mail;
        $this->e_mail_entry->OptionsAditional['style'] = array('border:1px solid black');
        
        $this->edit_hidden = new TEdit();
        $this->edit_hidden->Name = "id_client";
        $this->edit_hidden->Type = "text";
        $this->edit_hidden->Text = $_GET['id_client'];
        $this->edit_hidden->IsHidden = true;
        
        $this->button_cancel = new TButton("Cancelar");
        $this->button_cancel->Type = "reset";
        $this->button_cancel->Id = "cancelar";
        $this->button_cancel->Name = $this->button_cancel->Name;
        $this->button_cancel->OptionsAditional = array('style'=>array('font-weight:bold','cursor:pointer'));
        
        $this->button_guardar = new TButton("Actualizar");
        $this->button_guardar->Type = 'submit';
        $this->button_guardar->Name = "actualizar";
        $this->button_guardar->Id = $this->button_guardar->Name;
        $this->button_guardar->OptionsAditional = array('style'=>array('font-weight:bold','cursor:pointer'));
        
    }
    
    public function actionDelete(){
        if (isset($_GET['id_client'])){
            $client = client::model()->find("id_client=:id_client",array(":id_client"=>$_GET['id_client']));
            $client->active = 0;
            if ($client->update()){
                $this->redirec(VeCms::createUrl("./grid"),0,array("del"=>true));
            }
            else{
                throw new Exception("Error al intentar eliminar el cliente indicado");
            }
        }
        else{
            throw new Exception("Error al intentar eliminar el cliente indicado");
        }
    }
    
    public function actionView(){
        
        if (isset($_GET['id_client'])){
        
            $this->link_back = new TLink("Regresar");
                $js_link = "parent.history.back(); 
                            return false; 
                            ";
            $this->link_back->onClick = $js_link;
            $this->link_back->OptionsAditional['style'][] = "color:#A9BCF5";
            $this->link_back->OptionsAditional['style'][] = "font-weight:bold";

            $client = client::model()->find("id_client=:id_client",array(":id_client"=>$_GET['id_client']));
            
            $this->business_name_label = new TLabel("Nombre:");
            $this->business_name_label->OptionsAditional['style'][] = "font-weight:bold";
            $this->business_name_value = new TLabel($client->business_name);
            
            $this->rif_label = new TLabel("Rif:");
            $this->rif_label->OptionsAditional['style'][] = "font-weight:bold";
            $this->rif_value = new TLabel($client->rif);
            
            $this->address_label = new TLabel("Direccion:");
            $this->address_label->OptionsAditional['style'][] = "font-weight:bold";
            $this->address_value = new TLabel($client->address);
            
            $this->phone_label = new TLabel("Telefono:");
            $this->phone_label->OptionsAditional['style'][] = "font-weight:bold";
            $this->phone_value = new TLabel($client->phone);
            
            $this->phone_mobile_label = new TLabel("Celular:");
            $this->phone_mobile_label->OptionsAditional['style'][] = "font-weight:bold";
            $this->phone_mobile_value = new TLabel($client->phone_mobile);
            
            $this->e_mail_label = new TLabel("Correo:");
            $this->e_mail_label->OptionsAditional['style'][] = "font-weight:bold";
            $this->e_mail_value = new TLabel($client->e_mail);
            
        }
        else{
            throw new Exception("Error al buscar el cliente indicado");
        }
    }
    
    //listado de clientes remoto para la vista de presupuesto y factura
    
    public function actionGridremote(){
        
        Templete::$theme_name = 'PopUp';
        
        // llamamos el modelo
        
        $offset = (isset($_GET['offset']))?$_GET['offset']:0;
        $client = new client();
        $client->Offset = $offset;
        $client->Limit = 10;
        
        // creamosla lista de usuarios para los popup
        
        $this->grid_client_popup = new TTable();
        $this->grid_client_popup->Id = 'grid-client';
        $this->grid_client_popup->Columns = 4;
        $this->grid_client_popup->Rows = $client->Limit;
        $this->grid_client_popup->Width = '100%';
        $this->grid_client_popup->Headers = array('Nro','Nombre','Rif','Telefono');
        $this->grid_client_popup->RowsAlign[0] = "center";
        $this->grid_client_popup->WidthCell[0] = 25;
        $this->grid_client_popup->Pagination = new TPagination(array("offset"=>$client->Offset,"limit"=>$client->Limit,  "total"=>client::model()->Count("active=:active",array(":active"=>1))));
        
        $client_item = $client->findAll("active = :active", array(':active'=>1));
        $count = 1;
        foreach ($client_item as $key=>$value){
            
            //generando los link para las acciones
            
            $clientJSON = json_encode($value);
            
            $link_bussiness_name = new TLink($value->business_name);
            $link_bussiness_name->onClick = "add_client('".$clientJSON."')";
            $link_bussiness_name->OptionsAditional['style'][]='text-decoration:none';
            $link_bussiness_name->OptionsAditional['style'][]='color:black';
            
            $link_rif = new TLink($value->rif);
            $link_rif->onClick = "add_client('".$clientJSON."')";
            $link_rif->OptionsAditional['style'][]='text-decoration:none';
            $link_rif->OptionsAditional['style'][]='color:black';
            
            $link_phone = new TLink($value->phone);
            $link_phone->onClick = "add_client('".$clientJSON."')";
            $link_phone->OptionsAditional['style'][]='text-decoration:none';
            $link_phone->OptionsAditional['style'][]='color:black';
            
            $this->grid_client_popup->Items[]=array($count,$link_bussiness_name->show(),$link_rif->show(),$link_phone->show());
            $count++;
            
        }
    
    }
}
?>
